package multithread.framework.threadmethod;

import java.util.Iterator;
import java.util.List;

import multithread.framework.exception.ConfigurationException;

public class ThreadMethodCheckerImpl implements IThreadMethodChecker {

	@SuppressWarnings("rawtypes")
	@Override
	public void checkDuplicateGroup(List<IThreadMethod> threadMethods, ThreadMethod threadMethod) {
		for (Iterator iterator = threadMethods.iterator(); iterator.hasNext();) {
			ThreadMethodGroup threadMethodGroup = (ThreadMethodGroup) iterator.next();
			List<ThreadMethod> methods = threadMethodGroup.getThreadmethodList();
			for (Iterator iterator2 = methods.iterator(); iterator2.hasNext();) {
				ThreadMethod tempMethod = (ThreadMethod) iterator2.next();
				if (tempMethod.equals(threadMethod)) {
					String message = "ThreadMethod: "
							+ threadMethod.getIvokeMthod().getName()
							+ " already belongs to group:\""
							+ tempMethod.getThreadGroupFor().value()
							+ "\". Here we find "
							+ threadMethod.getIvokeMthod().getName()
							+ " belongs to another group: \""
							+ threadMethod.getThreadGroupFor().value()
							+ "\"."
							+ "One method can only belong to one group at a time, please check your configuration!";
					throw new ConfigurationException(message);
				}
			}
		}
	}

}
